// pages/positive/positive.js
const app = getApp();
var router = require("../../router.js");
var audioAction = {};
audioAction.method = 'pause';
var audioPay = {};
audioPay.method = 'play';
//音频播放
const backgroundAudioManager = wx.getBackgroundAudioManager();
const recorderManager = wx.getRecorderManager();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabBar: 1,//底部
    content: 1, //内容
    login: 0,
    list: [
    
    ],
    userType: 1,
  },
  start_liuyan: function (e) {
    var id = e.target.dataset.id;
    app.navTo('../liuyan/liuyan?id=' + id)
  },
  // 底部
  index: function () {
    app.radTo('../index/index')
  },
  dianzan: function () {
    app.radTo('../list/list')
  },
  my: function () {
    app.radTo('../my/my')
  },
  positive: function () {
    app.radTo('../positive/positive')
  },
  //音频播放
  start_chosedeil: function (event) {
    var that = this;
    var index = event.target.dataset.ctr;  //获取每个的id
    var list = that.data.list;
    console.log(list)
    for (var i = 0; i < list.length; i++) {
      if (i != index) {
        list[i].picB = true
      }
    }
    if (list[index].picB) {
      list[index].picB = false
    } else {
      list[index].picB = true
    }
    if (list[index].picB == false) {
      console.log('添加url')
      backgroundAudioManager.title = list[index].title;
      backgroundAudioManager.src = list[index].src;
    } else {
      console.log('暂停')
      backgroundAudioManager.pause();
    }
    this.setData({
      // picB: ic,
      index: index,
      list: list
    })
  },

  //分享
  onShareAppMessage: function (res) {  
    if (res.from === 'button') {
      var that = this;
      // 来自页面内转发按钮    
      var id = res.target.dataset.id;
      var index = res.target.dataset.ctr;
      var title = that.data.list[index].name;
      return {
        title: title,
        path: '/pages/positive/positive',
        success: function (res) {
          app.request({
            url: router.msg.share,
            method: "POST",
            data: {
              id: id,
            },
            success: function (data) {
              if (data) {
                var list = that.data.list;
                var share = list[index].share;
                list[index].share = share + 1;
                that.setData({
                  list: list
                })
                wx.showToast({
                  title: '分享成功',
                  icon: 'success',
                  duration: 2000
                });
              } else {
                wx.showToast({
                  title: '分享失败请重试',
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          });
        },
        fail: function (res) {
          wx.showToast({
            title: '分享已取消',
            icon: 'none',
            duration: 2000
          });
        }
      }
    }
  },

  lower: function (e) {
    this.getInfo();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    app.isLogin();
    wx.showShareMenu({
      withShareTicket: true
    })
    this.setData({
      height: wx.getSystemInfoSync().windowHeight
    });
  },
  onHide: function () {
    this.setData({
      info: false,
    });
  },
  onShow: function () {
    let that = this;
    app.request({
      url: router.msg.audit,
      method: "GET",
      success: function (data) {
        that.setData({
          login: data.login,
          content: data.content,
        })
      }
    });
    this.setData({
      scrollTop: 0,
      page: 0,
      list: []
    });
    wx.showShareMenu({
      withShareTicket: true
    })
    this.getInfo();
    //监听音乐时长
    backgroundAudioManager.onTimeUpdate(function () {
      var list = that.data.list;
      var index = that.data.index;    //索引
      var currentTime = backgroundAudioManager.currentTime;  //当前时长
      var currentTime_t = Math.floor(currentTime / 60) + ":" + (currentTime % 60 / 100).toFixed(2).slice(-2);
      console.log(currentTime_t, 'currentTime_t')
      for (var i = 0; i < list.length; i++) {
        list[index].currentTime = currentTime_t
      }
      that.setData({
        list: list
      })
    })
    //监听音乐自然结束
    backgroundAudioManager.onEnded(function () {
      var list = that.data.list;
      for (var i = 0; i < list.length; i++) {
        list[i].picB = true
        list[i].currentTime = '0:00'
      }
      that.setData({
        list: list
      })
    });
  },
  getInfo: function () {
    var that = this;
    app.request({
      url: router.msg.sayList,
      data: {
        uid: wx.getStorageSync('uid'),
        page: that.data.page,
        userType: that.data.userType,
        infoType: 1,
        orderBuy: 'id',
      },
      success: function (data) {
        console.log(data);        
        if (data.length > 0) {
          for (var i = 0; i < data.length; i++) {
            var obj = {};
            var praise = {};
            obj.title = data[i].nickname
            obj.data = data[i].createtime
            obj.pic = data[i].head
            obj.src = data[i].music
            obj.duration = data[i].duration
            obj.name = data[i].title
            obj.author = data[i].nickname
            obj.id = data[i].id
            obj.poster = data[i].img
            obj.share = data[i].share
            obj.currentTime = '0:00'
            obj.picB = true
            obj.praise = data[i].praise
            obj.praise_type = data[i].praise_type
            obj.collect = data[i].collect
            obj.collect_type = data[i].collect_type
            obj.comment = data[i].comment
            obj.audioAction = audioAction,
              that.data.list.push(obj);
          }
          that.setData({
            list: that.data.list,
            page: that.data.page + 1
          });
        } else {
          that.setData({
            info: true,
          });
        }
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
})